Business Analytics

Advanced Data Visualizations

Ayush Patel and Jayati Sharma

24 February, 2024

Pre-requisite

You already….

  • Know basic and advanced data wrangling functions in R
  • Know basics of data visualization in R
  • Can write functions in R

Before we begin

Please install and load the following packages

library(dplyr)
library(tidyverse)
library(patchwork)
library(ggiraph)
library(gghighlight)



Access lecture slide from the course landing page

About me

I am Ayush.

I am a researcher working at the intersection of data, law, development and economics.

I teach Data Science using R at Gokhale Institute of Politics and Economics

I am a RStudio (Posit) certified tidyverse Instructor.

I am a Researcher at Oxford Poverty and Human development Initiative (OPHI), at the University of Oxford.

Reach me

ayush.ap58@gmail.com

ayush.patel@gipe.ac.in

Learning Objectives

  • Learn annotation for graphs in R
  • Learn how to combine graphs
  • Learn scaling functions in R
  • Learn how to make ggplot graphs interactive

Annotations in ggplot

Content for this topic has been sourced from ggplot2. Please check out the work for detailed information.

  • In addition to plotting your graph, you want to provide additional details to explain your graph
  • Text annotations are useful in this case
  • The annotate() function can be used for any kind of geometric object
  • Let us begin by text annotations
  • In the annotate() function, the type of geom is specified first
  • Then, the positining is required (x and y coordinates in this case)
  • This is followed by the label
 ggplot(mtcars, aes(x = wt, y = mpg)) +
   geom_point()+
   annotate("text", x = 4, y = 25, label = "Annotation Text")

Annotations in ggplot

Content for this topic has been sourced from ggplot2. Please check out the work for detailed information.

  • Further, annotations can be customized
 ggplot(mtcars, aes(x = wt, y = mpg)) +
   geom_point()+
   annotate("text", x = 4, y = 25, label = "Annotation Text", colour = "orange", size = 8)

 ggplot(mtcars, aes(x = wt, y = mpg)) +
   geom_point()+
   annotate("text", x = 1:5, y = 6, label = "Annotation Text", colour = "orange", size = 3)

Annotations in ggplot

Content for this topic has been sourced from ggplot2. Please check out the work for detailed information.

  • Similar to text annotation, other geoms can be used for annotations
  • However, instead of x and y, xmin and xmax is used for coordinates of the rectangle
  • Do you remember what alpha is used for?
 ggplot(mtcars, aes(x = wt, y = mpg)) +
   geom_point()+
   annotate("rect", xmin = 4.8, xmax = 5.7, ymin = 10, ymax = 18.6, alpha = .2)

Annotations in ggplot

Content for this topic has been sourced from ggplot2. Please check out the work for detailed information.

  • Suppose you want to add a line segment to your graph
  • annotate() over here requires x and xend coordinates
 ggplot(mtcars, aes(x = wt, y = mpg)) +
   geom_point()+
   annotate("segment", x = 4.8, xend = 5.7, y = 10, yend = 18.6, colour = "red")

Scales